from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.core.security import get_current_user
from app.schemas.user import User, UserUpdate
from app.crud import user as user_crud

router = APIRouter()

@router.get("/me", response_model=User)
async def read_user_me(
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user)
):
    """获取当前用户信息"""
    user = user_crud.get_user(db, current_user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    return user

@router.put("/me", response_model=User)
async def update_user_me(
    *,
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user),
    user_in: UserUpdate
):
    """更新当前用户信息"""
    user = user_crud.get_user(db, current_user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    user = user_crud.update_user(db, db_obj=user, obj_in=user_in)
    return user 